View Javadoc
1   package org.apache.maven.surefire.testng;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import org.apache.maven.surefire.report.ConsoleOutputCapture;
28  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
29  import org.apache.maven.surefire.report.ReporterFactory;
30  import org.apache.maven.surefire.report.RunListener;
31  import org.apache.maven.surefire.testset.TestSetFailedException;
32  
33  /**
34   * Handles suite xml file definitions for TestNG.
35   *
36   * @author jkuhnert
37   * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
38   */
39  public class TestNGXmlTestSuite
40      implements TestNgTestSuite
41  {
42      private final List suiteFiles;
43  
44      private List<String> suiteFilePaths;
45  
46      private final String testSourceDirectory;
47  
48      private final Map<String, String> options;
49  
50      private final File reportsDirectory;
51  
52      private final int skipAfterFailureCount;
53  
54      // Not really used
55      private Map<File, String> testSets;
56  
57      /**
58       * Creates a testng testset to be configured by the specified
59       * xml file(s). The XML files are suite definitions files according to TestNG DTD.
60       */
61      public TestNGXmlTestSuite( List<File> suiteFiles, String testSourceDirectory, Map<String, String> confOptions,
62                                 File reportsDirectory, int skipAfterFailureCount )
63      {
64          this.suiteFiles = suiteFiles;
65          this.options = confOptions;
66          this.testSourceDirectory = testSourceDirectory;
67          this.reportsDirectory = reportsDirectory;
68          this.skipAfterFailureCount = skipAfterFailureCount;
69      }
70  
71      public void execute( ReporterFactory reporterManagerFactory )
72          throws TestSetFailedException
73      {
74          if ( testSets == null )
75          {
76              throw new IllegalStateException( "You must call locateTestSets before calling execute" );
77          }
78  //        RunListener reporter = new SynchronizedReporterManager( reporterManagerFactory.createReporter() );
79          RunListener reporter = reporterManagerFactory.createReporter();
80          ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
81  
82          TestNGDirectoryTestSuite.startTestSuite( reporter, this );
83          TestNGExecutor.run( suiteFilePaths, testSourceDirectory, options, reporter, this, reportsDirectory,
84                              skipAfterFailureCount );
85          TestNGDirectoryTestSuite.finishTestSuite( reporter, this );
86      }
87  
88      public void execute( String testSetName, ReporterFactory reporterManagerFactory )
89          throws TestSetFailedException
90      {
91          throw new TestSetFailedException( "Cannot run individual test when suite files are specified" );
92      }
93  
94      public Map locateTestSets( ClassLoader classLoader )
95          throws TestSetFailedException
96      {
97          if ( testSets != null )
98          {
99              throw new IllegalStateException( "You can't call locateTestSets twice" );
100         }
101 
102         if ( this.suiteFiles == null )
103         {
104             throw new IllegalStateException( "No suite files were specified" );
105         }
106 
107         this.testSets = new HashMap<File, String>();
108         this.suiteFilePaths = new ArrayList<String>();
109 
110         for ( Object suiteFile : suiteFiles )
111         {
112             File file = (File) suiteFile;
113             if ( !file.exists() || !file.isFile() )
114             {
115                 throw new TestSetFailedException( "Suite file " + file + " is not a valid file" );
116             }
117             this.testSets.put( file, file.getAbsolutePath() );
118             this.suiteFilePaths.add( file.getAbsolutePath() );
119         }
120 
121         return this.testSets;
122     }
123 
124     public String getSuiteName()
125     {
126         String result = options.get( "suitename" );
127         return result == null ? "TestSuite" : result;
128     }
129 }